package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.logistics.api.dto.request.LogisticsMemberIdAndRoleIdReq;
import com.ssy.lingxi.logistics.api.dto.request.LogisticsSelectCompanyListFeignReq;
import com.ssy.lingxi.logistics.api.dto.response.CommonSelectResponse;
import com.ssy.lingxi.logistics.api.dto.response.FreightTemplateFeignRes;
import com.ssy.lingxi.logistics.api.dto.response.ShipperAddressSelectFeignRes;
import com.ssy.lingxi.logistics.entity.Company;
import com.ssy.lingxi.logistics.entity.ShipperAddress;
import com.ssy.lingxi.logistics.model.enums.CommonStatusEnum;
import com.ssy.lingxi.logistics.repository.CompanyRepository;
import com.ssy.lingxi.logistics.repository.FreightTemplateRepository;
import com.ssy.lingxi.logistics.repository.ShipperAddressRepository;
import com.ssy.lingxi.logistics.service.ILogisticsSelectFeignService;
import com.ssy.lingxi.logistics.util.LogisticsAddressUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wyx
 * @version 2.0.0
 * @date 2022-03-29
 **/
@Service
public class LogisticsSelectFeignServiceImpl implements ILogisticsSelectFeignService {
    @Resource
    private FreightTemplateRepository freightTemplateRepository;
    @Resource
    private ShipperAddressRepository shipperAddressRepository;
    @Resource
    private CompanyRepository companyRepository;


    /**
     * 能力中心 - 运费模板下拉框
     * @return 返回结果
     */
    @Override
    public Wrapper<List<FreightTemplateFeignRes>> listFreightTemplate(LogisticsMemberIdAndRoleIdReq req) {
        List<FreightTemplateFeignRes> resultList = freightTemplateRepository.findAllByStatusAndMemberIdAndRoleId(CommonStatusEnum.ENABLE.getCode(), req.getMemberId(), req.getRoleId())
                .stream().map(e -> {
                    FreightTemplateFeignRes res = new FreightTemplateFeignRes();
                    res.setId(e.getId());
                    res.setName(e.getName());
                    return res;
                }).collect(Collectors.toList());
        return Wrapper.success(resultList);
    }

    /**
     * 发货地址查询全部(按照是否默认排序)
     * @return 返回结果
     */
    @Override
    public Wrapper<List<ShipperAddressSelectFeignRes>> listShipperAddress(LogisticsMemberIdAndRoleIdReq req) {
        List<Sort.Order> orders = Arrays.asList(new Sort.Order(Sort.Direction.DESC, "isDefault"), new Sort.Order(Sort.Direction.DESC, "createTime"));
        List<ShipperAddress> all = shipperAddressRepository.findAllByMemberIdAndRoleId(req.getMemberId(), req.getRoleId(), Sort.by(orders));
        List<ShipperAddressSelectFeignRes> resultList = all.stream().map(e -> {
            ShipperAddressSelectFeignRes res = new ShipperAddressSelectFeignRes();
            String fullAddress = LogisticsAddressUtil.contractFullAddress(e.getProvinceName(), e.getCityName(), e.getDistrictName(), e.getStreetName(), e.getAddress());
            res.setFullAddress(fullAddress);
            res.setId(e.getId());
            res.setShipperName(e.getShipperName());
            res.setProvinceCode(e.getProvinceCode());
            res.setProvinceName(e.getProvinceName());
            res.setCityCode(e.getCityCode());
            res.setCityName(e.getCityName());
            res.setDistrictCode(e.getDistrictCode());
            res.setDistrictName(e.getDistrictName());
            res.setStreetCode(e.getStreetCode());
            res.setStreetName(e.getStreetName());
            res.setAddress(e.getAddress());
            res.setPhone(e.getPhone());
            res.setIsDefault(e.getIsDefault());
            return res;
        }).collect(Collectors.toList());
        return Wrapper.success(resultList);
    }

    /**
     * 物流公司查询全部(不同合作类型)
     * @param req 接口参数
     * @return 返回结果
     */
    @Override
    public Wrapper<List<CommonSelectResponse>> listCompany(LogisticsSelectCompanyListFeignReq req) {
        List<Company> all = companyRepository.findAll((Specification<Company>) (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            predicateList.add(cb.equal(root.get("memberId"), req.getMemberId()));
            predicateList.add(cb.equal(root.get("roleId"), req.getRoleId()));
            predicateList.add(cb.equal(root.get("status"), CommonStatusEnum.ENABLE.getCode()));
            if (Objects.nonNull(req.getCooperateType())) {
                predicateList.add(cb.equal(root.get("cooperateType"), req.getCooperateType()));
            }

            Predicate[] p = new Predicate[predicateList.size()];
            return query.where(predicateList.toArray(p)).getRestriction();
        });

        List<CommonSelectResponse> resultList = all.stream().map(e -> {
            CommonSelectResponse commonSelectResponse = new CommonSelectResponse();
            commonSelectResponse.setId(e.getId());
            commonSelectResponse.setName(e.getName());
            return commonSelectResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(resultList);
    }
}
